package com.sdy.healthcode.web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.healthcode.biz.dto.Address;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.dto.PersonMana;
import com.sdy.healthcode.biz.model.ApplyPost;
import com.sdy.healthcode.biz.model.ReportPeoplePass;
import com.sdy.healthcode.biz.service.ApplyPostService;
import com.sdy.healthcode.biz.service.ReportPeoplePassService;
import com.sdy.healthcode.biz.service.UserActionService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * <p>
 * 人员管理 前端控制器
 * </p>
 *
 * @author cgj
 * @since 2020-02-28
 */
@Slf4j
@RestController
@RequestMapping("/personnel")
@Api(tags = "人员管理")
public class PersonnelController extends BaseController {

    @Autowired
    private ApplyPostService applyPostService;
    @Autowired
    private ReportPeoplePassService reportPeoplePassService;

    @GetMapping("/personnelManaPage")
    @ApiOperation(value = "人员管理")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "type", value = "人员类型"),
            @ApiImplicitParam(name = "healthAbnormalDetail", value = "体温状况"),
            @ApiImplicitParam(name = "name", value = "姓名"),
            @ApiImplicitParam(name = "starTime", value = "开始就时间"),
            @ApiImplicitParam(name = "endTime", value = "结束就时间")
    })
    public Response personnelManaPage(Integer type, Integer healthStatus, String name, String startTime, String endTime,
                                      Page<ReportPeoplePass> page, HttpServletRequest request) throws UnsupportedEncodingException {

        // 时间条件
        String creatTime = null;
        String overTime = null;
        if (StringUtil.isNotBlank(startTime)) {
            creatTime = startTime + " 00:00:00";
        }
        if (StringUtil.isNotBlank(endTime)) {
            overTime = endTime + " 23:59:59";
        }
        if (StringUtil.isNotBlank(name)) {
            name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        }
        type = type == null ? -1 : type;
        healthStatus = healthStatus == null ? -1 : healthStatus;
        IPage<ReportPeoplePass> r = reportPeoplePassService.page(page, new LambdaQueryWrapper<ReportPeoplePass>()
                .eq(type != -1, ReportPeoplePass::getPeopleType, type == 1 ? "患者" : type == 2 ? "家属" : "本院人员")
                .like(StringUtil.isNotBlank(name), ReportPeoplePass::getName, name)
                .eq(healthStatus != -1, ReportPeoplePass::getHealthType, healthStatus == 1 ? "正常" : "发热")
                .ge(creatTime != null, ReportPeoplePass::getRecordTime, creatTime)
                .le(overTime != null, ReportPeoplePass::getRecordTime, overTime)
                .orderByDesc(ReportPeoplePass::getRecordTime, ReportPeoplePass::getId));
        for (ReportPeoplePass record : r.getRecords()) {
            if (StringUtil.isNotBlank(record.getTripAddr())) {
                Address addr = Address.fromStrToSingleAddr(record.getTripAddr());
                record.setTripAddr(addr == null ? "" : addr.getArea() + " " + addr.getAddr());
            }
        }
        return Response.success(r);
    }
}
